<script>

var passGerente = false;
var qtdAtual = 0;
var cod_ped = 0;
var bt_obj;
var vsrc = 130;
var btn_click = false;

function moveDown()
{
	var scrtop = document.getElementById("lisProdDiv").scrollTop;

	document.getElementById("lisProdDiv").scrollTop = scrtop + vsrc;
}

function moveUp()
{
	var scrtop = document.getElementById("lisProdDiv").scrollTop;

	document.getElementById("lisProdDiv").scrollTop = scrtop - vsrc;
}

function addMUnDiv(un, bt)
{
	if(!(aux = parseInt(un)))
	{
		aux = 1;
	}

	addUnDiv(aux, bt);
}

function getGerente()
{
	if(auxJson.caf01_admin != 0)
	{
		$(".manager").fadeOut("fast");

		passGerente = true;

		resetPassword();

		$(bt_obj).val(qtdAtual);

		if(qtdAtual == 0)
		{
			$.post('<?php echo $this->base; ?>/pedido/deleteqtd', {cod_menu: $("#subgroups").val(), cod: cod_ped, cod_item: auxPedido, un: qtdAtual}, function(req){
				listProd();
			});

			return;
		}

		$.post('<?php echo $this->base; ?>/pedido/saveqtd', {cod_menu: $("#subgroups").val(), cod: cod_ped, cod_item: auxPedido, un: qtdAtual}, function(req){
			listProd();
		});

		return true;
	}
	else
	{
		alert("Senha de Gerente invalida.");

		passGerente = false;

		resetPassword();

		return false;
	}
}

function openUn(cod)
{
	//$(".bt_" + cod).select();
	$(".un_" + cod).slideDown("fast");
	btn_click = true;
	//$(".bt_" + cod).slideDown("fast");
}

function filter(a)
{
	$(".subprod").hide();
	document.getElementById("lisProdDiv").scrollTop = 0;
	$("." + a).fadeIn("fast");
}

function addUnDiv(un, bt)
{
	auxQtd = parseInt(un);

	$(".bt_" + bt).val(auxQtd).focus().blur();
}

function addQtdProd(cod, un, unAtual, obj)
{
	qtd = parseInt(un);
	qtdAtual = qtd;
	cod_ped = cod;
	bt_obj = obj;

	//alert(edpedido);

	if(btn_click)
	{
		btn_click = false;

		return;
	}
	
	if(edpedido)
	{
		if(qtd < unAtual)
		{
			$(".manager").fadeIn('fast');
			
			//alert("Quantidade invalida.");
	
			$(obj).val(unAtual);
	
			return false;
		}
	}

	if(qtd == 0)
	{
		$.post('<?php echo $this->base; ?>/pedido/deleteqtd', {cod_menu: $("#subgroups").val(), cod: cod, cod_item: auxPedido, un: qtd}, function(req){
			listProd();
		});

		return;
	}
	
	if(qtd)
	{
		$(".bt_" + cod).val(qtd);
		
		$.post('<?php echo $this->base; ?>/pedido/saveqtd', {cod_menu: $("#subgroups").val(), cod: cod, cod_item: auxPedido, un: qtd}, function(req){
			listProd();
		});
	}

	$("#exclDiv_" + cod_ped).show();
}

function showComentario(id)
{
	var qtd_btn = $('.bt_' + id).val();
	var obj = 0;

	if(!qtd_btn)
	{
		if(confirm("Nenhum item adicionado. Adicionar?"))
		{
			addQtdProd(id, 1, 0, obj);
		}
		else
		{
			return false;
		}
	}
	
	$.post('<?php echo $this->base; ?>/product/comentario', {cod: id, cod_item: auxPedido}, function(res){
		$("#comentDiv").html(res);
	});	

	return;
}

</script>

<div id="comentDiv"></div>

<div class="ui-corner-all" style="position: absolute; opacity: 0.8; margin-left: 541px; margin-top: 39px; width: 32px; background: #DFDFDF;">
	<div onclick="moveUp();" class="ui-corner-top" style="padding-top: 5px; padding-bottom: 3px; background: #CFCFCF">
		<img src="<?php echo $this->base; ?>/public/images/actions/up.png" />
	</div>
	<div onclick="moveDown();" class="ui-corner-bottom" style="background: #CFCFCF; margin-top: 251px; padding-top: 7px;">
		<img src="<?php echo $this->base; ?>/public/images/actions/down.png" />
	</div>
</div>

<?php 

if($this->arr_prod)
{
	$arr_prod = $this->arr_prod;
	$menu = $arr_prod[0]["caf03_menu"];
}
else
{
	die("<i>Vazio</i>");
}


echo "<label style='font-weight: bold'>$menu</label>";
echo "<label id='load_subproduct' style='font-weight: bold; display: none;'>&nbsp;<img src='$this->base/public/images/ajax-loader.gif' /></label>";

switch ($menu)
{
	case "Soft Drinks":
?>
	<div style="text-align: center; float: right;">
		<a href="#" style="color: blue;  padding: 10px; text-decoration: none;" onclick="javascript:$('.subprod').hide();$('.subprod').fadeIn('fast');" >All</a>
		|
		<a href="#" style="color: blue; padding: 10px; text-decoration: none;" onclick="javascript:filter('Lata');" >Lata</a>
		|
		<a href="#" style="color: blue; padding: 10px; text-decoration: none;" onclick="javascript:filter('600ml');">600ml</a>
		|
		<a href="#" style="color: blue; padding: 10px; text-decoration: none;" onclick="javascript:filter('Copo');" >Copo</a>
	</div>
<?
	break;
	
	case "Pizza":
?>
	<div style="text-align: center; float: right;">
		<a href="#" style="color: blue; padding: 10px; text-decoration: none;" onclick="javascript:$('.subprod').fadeIn('fast');" >All</a>
		|
		<a href="#" style="color: blue; padding: 10px; text-decoration: none;" onclick="javascript:filter('Small');" >Small</a>
		|
		<a href="#" style="color: blue; padding: 10px; text-decoration: none;" onclick="javascript:filter('Medium');">Medium</a>
		|
		<a href="#" style="color: blue; padding: 10px; text-decoration: none;" onclick="javascript:filter('Large');" >Large</a>
	</div>
<?
	break;
}

?>

<hr style="margin-top: 8px; margin-bottom: 8px;" />

<div id="lisProdDiv" style="height: 345px; overflow: hidden;">

	<?php 
	
	if(is_array($arr_prod))
	{
		foreach ($arr_prod as $key => $val)
		{
			$color = "";
			
			if($this->edit && $val["caf04_qtd"])
			{
				$color = "border: 1px solid red;";
			}
			else
			{
				$color = "border: 1px solid gray;";
			}
		?>
			<div class="<?php echo $val["caf05_car"]; ?> subprod" style="float: left; height: 120px; width: 125px; margin: 5px; text-align: center;">
				<div style="position: relative;">
					<div class="ui-corner-all button" style="width: 120px; height: 80px; border: 1px solid gray;" onclick="addMUnDiv((parseInt($('.bt_<?php echo $val["caf05_cod"]; ?>').val()) + 1), '<?php echo $val["caf05_cod"]; ?>');" >
						<div style="margin: 3px; padding-top: 4px;">
						<?php echo "{$val["caf05_prod"]}<br />({$val["caf05_car"]})"; ?>
						<hr style="margin: 3px;" />
						$<?php echo $val["caf05_valor"]; ?>/un
						</div>
					</div>
				</div>
				<div style="width: 120px; margin-top: 1px; text-align: center;">
					<input class="bt_<?php echo $val["caf05_cod"]; ?> ui-corner-all" onblur="$('.un_<?php echo $val["caf05_cod"]; ?>').slideUp(); addQtdProd('<?php echo $val["caf05_cod"]; ?>', $(this).val(), '<?php echo $val["caf04_qtd"]; ?>', this);" onclick="openUn('<?php echo $val["caf05_cod"]; ?>');" type="text" value="<?php echo $val["caf04_qtd"]; ?>" style="<?php echo $color; ?> font-size: 18px; padding: 5px 0 5px 0; height: 25px; width: 120px;" />
				</div>
				<div style="opacity: 0.9; z-index: 999; position: absolute; display: none; text-align: left; font-size: 24px; width: 166px; border: 1px solid #B0ADAD; background-color: white; margin-left: 3px;" class="ui-widget ui-corner-all unit un_<?php echo $val["caf05_cod"]; ?>" >
					<div class="ui-corner-top" onclick="showComentario(<?php echo $val["caf05_cod"]; ?>);" style="border-bottom: 1px solid gray; padding: 5px; background-color: #EFEFEF;">
						<div style="float: left; margin: 0px 3px 0px 0px;">
							<img src="<?php echo $this->base; ?>/public/images/actions/msg.png" /> 
						</div>
						<div style="margin-top: 2px;">
							Mensagem
						</div>
					</div>
					<?php 
					$excl_btn = "";
					
					if(!$val["caf04_qtd"])
					{
						$excl_btn = "display: none;";
					}
					
					?>
					<div id="exclDiv_<?php echo $val["caf05_cod"]; ?>" class="ui-corner-top" onclick="addUnDiv(0, '<?php echo $val["caf05_cod"]; ?>');" style="<?php echo $excl_btn; ?> border-bottom: 1px solid gray; padding: 5px; background-color: #EFEFEF;">
						<div style="float: left; margin: 0px 3px 0px 0px;">
							<img src="<?php echo $this->base; ?>/public/images/actions/delete.png" /> 
						</div>
						<div style="margin-top: 2px;">
							Excluir
						</div>
					</div>
					<div onclick="addUnDiv($(this).html(), '<?php echo $val["caf05_cod"]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
						1
					</div>
					<div onclick="addUnDiv($(this).html(), '<?php echo $val["caf05_cod"]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
						2
					</div>
					<div onclick="addUnDiv($(this).html(), '<?php echo $val["caf05_cod"]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
						3
					</div>
					<div onclick="addUnDiv($(this).html(), '<?php echo $val["caf05_cod"]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
						4
					</div>
					<div onclick="addUnDiv($(this).html(), '<?php echo $val["caf05_cod"]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
						5
					</div>
					<div onclick="addUnDiv($(this).html(), '<?php echo $val["caf05_cod"]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
						6
					</div>
				</div>
				<!-- 
				<select onclick="addQtdProd('<?php echo $val["caf05_cod"]; ?>', this);" style="font-size: 18px; padding: 5px 0 5px 0; height: 40px; width: 170px;">
					<option value="">Un</option>
					<?php 
					for($i = 1; $i < 15; $i++)
					{
						$select = "";
						
						if($val["caf04_qtd"] == $i)
						{
							$select = 'selected="selected"';
						}
					?>
					<option <?php echo $select; ?> ><?php echo $i; ?></option>				
					<?
					}
					?>
				</select>
				-->
			</div>
		<?php 
		}
		?>
	<?php 
	}
	else 
	{
	?>
	<i>Nenhum produto</i>
	<?php 
	}
	?>

</div>

<div class="manager" style="opacity: 0.9; display: none; position: fixed; width: 100%; height: 100%; background-color: white; top: 0; left: 0;"></div>
<div class="manager" style="display: none; position: fixed; width: 100%; height: 100%; top: 0; left: 0;">
	<div class="ui-corner-all" style="padding: 5px; text-align: center; color: red; background-color: white; width: 200px; margin: 0 auto; margin-top: 10px; margin-bottom: 10px;">
		Sem permissao.
		<br />
		Digite a senha do gerente.
		<br />
	</div>
	<div style="margin: 0 auto; width: 380px; text-align: right;">
		<?php echo $this->partial("helpers/password.phtml", array("jsfunc" => "getGerente()", "base" => $this->base)); ?>
		<hr />
		<input type="button" value="Cancelar" style="width: 380px; height: 30px;" onclick="$('.manager').hide();" />
	</div>
</div>